package com.xuexiang.xuidemo.provider;

import com.xuexiang.xuidemo.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ButtonMainAdapter extends BaseItemProvider {

    private List<Bean> beans = new ArrayList<>();

    public ButtonMainAdapter() {
        beans.add(new Bean("统一的按钮样式"));
        beans.add(new Bean("ShadowButton", "点击自带阴影效果的按钮"));
        beans.add(new Bean("ShadowView", "可设置阴影效果的控件"));
        beans.add(new Bean("RoundButton", "圆角按钮"));
        beans.add(new Bean("ButtonView", "通用按钮"));
        beans.add(new Bean("SwitchButton", "切换按钮"));
        beans.add(new Bean("RippleView", "点击水波纹样式"));
        beans.add(new Bean("SwitchIconView", "可切换图标的按钮"));
        beans.add(new Bean("SmoothCheckBox", "自带切换动画的CheckBox"));
        beans.add(new Bean("GoodView", "按钮点击漂浮效果"));
        beans.add(new Bean("ShineButton", "增强效果的按钮，自带闪烁的特效"));
    }

    @Override
    public int getCount() {
        return beans.size();
    }

    @Override
    public Object getItem(int position) {
        return beans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Holder holder;

        if (component == null) {
            Context context = componentContainer.getContext();
            holder = new Holder();
            component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_button_layout, null, false);
            holder.title = (Text) component.findComponentById(ResourceTable.Id_title);
            holder.descTitle = (Text) component.findComponentById(ResourceTable.Id_desc_title);
            holder.division = component.findComponentById(ResourceTable.Id_division);
            component.setTag(holder);
        } else {
            holder = (Holder) component.getTag();
        }

        Bean bean = beans.get(position);
        if (bean.desc == null || Objects.equals(bean.desc, "")) {
            holder.descTitle.setVisibility(Component.HIDE);
        } else {
            holder.descTitle.setText(bean.desc);
            holder.descTitle.setVisibility(Component.VISIBLE);
        }
        if (position == beans.size() - 1) {
            holder.division.setVisibility(Component.HIDE);
        } else {
            holder.division.setVisibility(Component.VISIBLE);
        }
        holder.title.setText(bean.title);
        return component;
    }


    static class Bean {
        String title;
        String desc;

        public Bean(String title) {
            this.title = title;
        }

        public Bean(String title, String desc) {
            this.title = title;
            this.desc = desc;
        }
    }

    public static class Holder {
        Text title;
        Text descTitle;
        Component division;
    }

}
